Contents | Index | < Browse | Browse >

LETTERstreambufULETTER A base class for stream buffers.

Overview
#include <iostream.h>

class streambuf {
protected:
streambuf();
streambuf(char *, int);
public:
virtual ~streambuf();
int in_avail();
int out_waiting();
int sbumpc();
int sgetc();
int sgetn(char *, int);
int snextc();
void stossc();
int sputbackc(char c);
int sputc(int c);
int sputn(const char *,int);
virtual int sync();
virtual streampos seekoff(streamoff, ios::seek_dir, int = ios::in|ios::out);
virtual streampos seekpos(streampos p, int mode = ios::in|ios::out);
virtual streambuf *setbuf(char *, size_t);
protected:
void setbuffer(char *, unsigned long n, int dynamic = 0);
int allocate();
int unbuffered();
void unbuffered(int i);
virtual int overflow(int = EOF);
virtual int underflow();
virtual int xsputn(const char *,int);
virtual int xsgetn(char *,int);
virtual int pbackfail(int);
virtual int doallocate();
void pbump(int i);
void gbump(int i);
};

Portability
AT&T Release 2 streams library

Description
This class is the base class for stream buffers. It already provides the buffer's date structure implementation so that its derived classes only need to provide the functions required for buffer over- and underflow situations. Due to this early implementation the methods
for reading and writing a char are under normal circumstances, that is, if no over- or underflow occurs, very fast.

Constructors
streambuf::streambuf();
Initializes the buffer for read and write access.

streambuf::streambuf(char *, int);
Initializes the buffer for read and write access using a predefined memory area of fixed size.

Deconstructors
streambuf::~streambuf();
Synchronizes the buffer with the physical output media.

Methods
int streambuf::in_avail();
Returns the number of characters in the buffer which have not yet been read.

int streambuf::out_waiting();
Returns the number of characters in the buffer which have not yet been written to the physical output media.

int streambuf::sbumpc();
Reads a character and returns EOF if an error occured or the file's end has been reached. Increases the read pointer by one.

int streambuf::sgetc();
Reads a character or EOF. The read pointer will not be modified.

int streambuf::sgetn(char *, int n);
Writes up to n characters to the char array and returns the actual number of characters read.

int streambuf::snextc();
Increases the read pointer by one and returns this position's character.

void streambuf::stossc();
Increases the read pointer by one.

int streambuf::sputbackc(char c);
Pushes a character back into the buffer or the stream. "c" must equal the last character read. You can push back multiple characters each-by-each, however the last character read must also be the first pushed back. Returns EOF, if the character could not be pushed back. Most streams allow to push back at least one character.

int streambuf::sputc(int c);
Writes a character and increases the write pointer by one. Returns c if successful and EOF otherwise.

int streambuf::sputn(const char *,int n);
Writes up to n characters from the char array and returns the number of characters actually written. With most streams, if the number returned is lower than n you can expect an error to have occured.

int streambuf::sync();
Synchronizes the buffer and the physical output media.

streampos streambuf::seekoff(streamoff, ios::seek_dir, int = ios::in|ios::out);
Sets the read, the write or both pointers to the specified position relative to the specified base position. Some streams recognize the read and the write pointer, in these cases the mode argument is ignored.

streampos streambuf::seekpos(streampos p, int mode = ios::in|ios::out);
Sets the read, the write or both pointers to the specified position. Some streams recognize the read and the write pointer, in these cases the mode argument is ignored.

streambuf *streambuf::setbuf(char *, size_t);
Sets the memory area for the buffer to the specified address and size. If successful this method returns the pointer to the streambuf object, otherwise NULL.

void streambuf::setbuffer(char *, unsigned long n, int dynamic = 0);
Sets the memory area for the buffer and initializes it as an empty buffer. dynamic must be 1 if the memory area was allocated using new and should be automatically freed using delete.

int streambuf::allocate();
If necessary, allocates a buffer using streambuf::doallocate().

int streambuf::unbuffered();
Returns 1 if the stream is unbuffered and 0 otherwise.

void streambuf::unbuffered(int i);
If i is 1, the stream is switched to unbuffered mode. This mode is slower than the buffer mode with most streams and should be avoided if possible.

int streambuf::overflow(int = EOF);
This method is called if the buffer is full for writing. Depending on the actual stream the buffer must then be transferred to the output media and the character passed must be put into the buffer. In unbuffered mode, this method is called for each single character. The character must then be directly transferred to the output media.

int streambuf::underflow();
This method is called if the buffer is empty for reading and needs to be refilled. It then returns the first character read. In unbuffered mode, every character must be read for its own.

int streambuf::xsputn(const char *,int n);
This method is used if a char array of the length n could not be copied to the buffer as a whole and needs to be copied piece-by-piece.

int streambuf::xsgetn(char *,int);
This method is called if the buffer does not contain enough characters for the char array and needs to be read in multiple passes.

int streambuf::pbackfail(int);
This method is called if the buffer is unable to accept a back-pushed character (eg. if the buffer is full). It returns the character if successful and EOF otherwise.

int streambuf::doallocate();
This method allocates a memory area, which has been allocated using new, as a new buffer.

See also
filebuf